package com.btpj.blog_server.controller.admin;

import com.btpj.blog_server.entity.Article;
import com.btpj.blog_server.entity.BaseResp;
import com.btpj.blog_server.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    ArticleService articleService;

    @GetMapping("/article/all")
    public BaseResp<List<Article>> getAllArticlesByAdmin(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                         @RequestParam(value = "pageCount", defaultValue = "6") Integer pageCount,
                                                         String keywords, Integer state) {
        BaseResp<List<Article>> baseResp = new BaseResp<>();
        baseResp.setStatus(0);
        baseResp.setMsg("获取文章列表成功");
        baseResp.setPage(page);
        baseResp.setData(articleService.getAllArticlesByState(state, keywords, page, pageCount, true));
        baseResp.setPageCount(pageCount);
        baseResp.setTotalCount(articleService.getArticleCountByState(state, keywords, true));
        return baseResp;
    }

    @GetMapping("/article/delete")
    public BaseResp<Boolean> deleteArticlesByAdmin(Long[] articleIds) {
        if (articleService.updateArticlesStatesById(articleIds, 0) == articleIds.length) {
            return new BaseResp<>(0, "删除成功", true);
        } else {
            return new BaseResp<>(1, "删除失败", false);
        }
    }
}
